/**
 * 定义监控数据模块
 */
(function(window, angular, undefined){

	var module = angular.module('water.entity.monitorData',['water.common.dataPage']);
	
	module.service('$monitorDataService',function($q,$waterDataCommon,$dataPage){
		
		/**
		 * 实时数据
		 * queryObj:
		 * {
		 * 		MN:
		 * 		PARMS:
		 * 		START:
		 * 		END:
		 * }
		 */
		this.realTimeListGetter = function(queryObj,onSuccess){
			var dataGetter = _commonListGetter("/MonitorData/getRealTimeData",queryObj,2,onSuccess);
			return dataGetter;
		}
		
		/**
		 * 分钟数据
		 * queryObj:
		 * {
		 * 		MN:
		 * 		PARMS:
		 * 		START:
		 * 		END:
		 * }
		 */
		this.minuteListGetter = function(queryObj,onSuccess){
			var dataGetter = _commonListGetter("/MonitorData/getMinuteData",queryObj,2,onSuccess);
			return dataGetter;
		}
		
		/**
		 * 小时数据
		 * queryObj:
		 * {
		 * 		MN:
		 * 		PARMS:
		 * 		START:
		 * 		END:
		 * }
		 */
		this.hourListGetter = function(queryObj,onSuccess){
			var dataGetter = _commonListGetter("/MonitorData/getHourData",queryObj,48,onSuccess);
			return dataGetter;
		}
		
		/**
		 * 天数据
		 * queryObj:
		 * {
		 * 		MN:
		 * 		PARMS:
		 * 		START:
		 * 		END:
		 * }
		 */
		this.dayListGetter = function(queryObj,onSuccess){
			var dataGetter = _commonListGetter("/MonitorData/getDayData",queryObj,744,onSuccess);
			return dataGetter;
		}
		/**
		 * 月数据
		 * queryObj:
		 * {
		 * 		MN:
		 * 		PARMS:
		 * 		START:
		 * 		END:
		 * }
		 */
		this.monthListGetter = function(queryObj,onSuccess){
			var dataGetter = _commonListGetter("/MonitorData/getMonthData",queryObj,8760,onSuccess);
			return dataGetter;
		}
		/**
		 * 周期数据
		 * queryObj:
		 * {
		 * 		MN:
		 * 		PARMS:
		 * 		START:
		 * 		END:
		 * }
		 */
		this.cycleListGetter = function(queryObj,onSuccess){
			var dataGetter = _commonListGetter("/MonitorData/getCycleData",queryObj,48,onSuccess);
			return dataGetter;
		}
		/**
		 * 季度数据
		 * queryObj:
		 * {
		 * 		MN:
		 * 		PARMS:
		 * 		START:
		 * 		END:
		 * }
		 */
		this.quarterListGetter = function(queryObj,onSuccess){
			var dataGetter = _commonListGetter("/MonitorData/getQuarterData",queryObj,8760,onSuccess);
			return dataGetter;
		}
		/**
		 * 年度数据
		 * queryObj:
		 * {
		 * 		MN:
		 * 		PARMS:
		 * 		START:
		 * 		END:
		 * }
		 */
		this.yearListGetter = function(queryObj,onSuccess){
			var dataGetter = _commonListGetter("/MonitorData/getYearData",queryObj,8760,onSuccess);
			return dataGetter;
		}
		
		/**
		 * 日报表
		 * queryObj:
		 * {
		 * 		MN:
		 * 		PARMS:
		 * 		START:
		 * 		END:
		 * }
		 */
		this.getDayReport = function(queryObj,onSuccess){
			var promise = $waterDataCommon.post("/MonitorData/getHourData",queryObj).then(function(data){
				data = _generateLimitInfo(data);
				return data;
			});
			return promise;
		}
		
		/**
		 * 周报表
		 * queryObj:
		 * {
		 * 		MN:
		 * 		PARMS:
		 * 		START:
		 * 		END:
		 * }
		 */
		this.getWeekReport = function(queryObj,onSuccess){
			var promise = $waterDataCommon.post("/MonitorData/getWeekData",queryObj).then(function(data){
				data = _generateLimitInfo(data);
				return data;
			});
			return promise;
		}
		
		/**
		 * 月报表
		 * queryObj:
		 * {
		 * 		MN:
		 * 		PARMS:
		 * 		START:
		 * 		END:
		 * }
		 */
		this.getMonthReport = function(queryObj,onSuccess){
			var promise = $waterDataCommon.post("/MonitorData/getMonthData",queryObj).then(function(data){
				data = _generateLimitInfo(data);
				return data;
			});
			return promise;
		}
		
		/**
		 * 季报表
		 * queryObj:
		 * {
		 * 		MN:
		 * 		PARMS:
		 * 		START:
		 * 		END:
		 * }
		 */
		this.getQuarterReport = function(queryObj,onSuccess){
			var promise = $waterDataCommon.post("/MonitorData/getQuarterData",queryObj).then(function(data){
				data = _generateLimitInfo(data);
				return data;
			});
			return promise;
		}
		
		/**
		 * 导出分钟数据excel，参数格式同上
		 */
		this.exportMinExcel = function(queryObj){
			queryObj._format = "excel";
			var queryString = $waterDataCommon.jsonTostring(queryObj);
			window.open(getBasePath()+"/MonitorData/getMinuteData.do?"+queryString);
		}
		
		/**
		 * 导出小时数据excel，参数格式同上
		 */
		this.exportHourExcel = function(queryObj){
			queryObj._format = "excel";
			var queryString = $waterDataCommon.jsonTostring(queryObj);
			window.open(getBasePath()+"/MonitorData/getHourData.do?"+queryString);
		}
		/**
		 * 导出周期数据excel，参数格式同上
		 */
		this.exportCycleExcel = function(queryObj){
			queryObj._format = "excel";
			var queryString = $waterDataCommon.jsonTostring(queryObj);
			window.open(getBasePath()+"/MonitorData/getCycleData.do?"+queryString);
		}
		
		/**
		 * 导出天数据excel，参数格式同上
		 */
		this.exportDayExcel = function(queryObj){
			queryObj._format = "excel";
			var queryString = $waterDataCommon.jsonTostring(queryObj);
			window.open(getBasePath()+"/MonitorData/getDayData.do?"+queryString);
		}
		
		/**
		 * 导出天数据excel，参数格式同上
		 */
		this.exportDailyExcel = function(queryObj){
			queryObj._format = "excel";
			var queryString = $waterDataCommon.jsonTostring(queryObj);
			window.open(getBasePath()+"/MonitorData/getData.do?"+queryString);
		}
		
		/**
		 * 导出周数据excel，参数格式同上
		 */
		this.exportWeekExcel = function(queryObj){
			queryObj._format = "excel";
			var queryString = $waterDataCommon.jsonTostring(queryObj);
			window.open(getBasePath()+"/MonitorData/getWeekData.do?"+queryString);
		}
		
		/**
		 * 导出月数据excel，参数格式同上
		 */
		this.exportMonthExcel = function(queryObj){
			queryObj._format = "excel";
			var queryString = $waterDataCommon.jsonTostring(queryObj);
			window.open(getBasePath()+"/MonitorData/getMonthData.do?"+queryString);
		}
		
		/**
		 * 导出季数据excel，参数格式同上
		 */
		this.exportQuarterExcel = function(queryObj){
			queryObj._format = "excel";
			var queryString = $waterDataCommon.jsonTostring(queryObj);
			window.open(getBasePath()+"/MonitorData/getQuarterData.do?"+queryString);
		}
		/**
		 * 导出年数据excel，参数格式同上
		 */
		this.exportYearExcel = function(queryObj){
			queryObj._format = "excel";
			var queryString = $waterDataCommon.jsonTostring(queryObj);
			window.open(getBasePath()+"/MonitorData/getYearData.do?"+queryString);
		}
		
		/**
		 * 通用的数据获取方法
		 * 实时数据，分钟，小时，周期，天公用，报表除外
		 * url:查询的接口地址
		 * queryObj:查询参数
		 * hourPrePage:每一页的小时数
		 * onSuccess:回调
		 */
		function _commonListGetter(url,queryObj,hourPrePage,onSuccess){
			var dataGetter = $dataPage.datetimePage({
				url:url,
				queryObj:queryObj,
				onSuccess:function(data){
					data = _generateLimitInfo(data);
					if(onSuccess){
						onSuccess(data)
					}
				},
				startDateField:'START',
				endDateField:'END',
				hourPrePage:hourPrePage
			});
			return dataGetter;
		}
		
		/**
		 * 处理上下限信息，处理成界面上可以显示的格式
		 * 比如 低限1，高限2，单位mg/l,处理成 1~2[mg/L]
		 * 		低限1,单位mg/l,处理成 >=1[mg/L]
		 * 		高限1,单位mg/l,处理成 <=1[mg/L]
		 * 		无限,单位mg/l,处理成 [mg/L]
		 */
		function _generateLimitInfo(data){
			var pollInfos = data.pollInfo;
			var limitInfos = {};
			for(parm in pollInfos){
				var pollItem = pollInfos[parm];
				var down = pollItem["down"];
				var up = pollItem["up"];
				var unit = pollItem["unit"];
				//判断污水瞬时流量单位
				if(getWaterType()=='0'){
						if(parm=='B01'){
						unit="升/秒";
					}
				}
				
				if(!_isEmpty(down) && !_isEmpty(up)){
					pollItem.limitText = down + "~" + up;
				}
				else if(!_isEmpty(down)){
					pollItem.limitText = ">" + down;
				}
				else if(!_isEmpty(up)){
					pollItem.limitText = "<" + up;
				}
				else{
					pollItem.limitText = "";
				}
				pollItem.limitText = pollItem.limitText + "["+unit+"]";
			}
			return data;
		}
		
		function _isEmpty(value){
			if(value == null ||
					value == "" ||
					typeof value === "undefined"){
				return true;
			}
			else{
				return false;
			}
		}
		
	})
}(window, angular));